/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCargarRnv.java
 *
 * Created on 27/06/2011, 08:48:43 PM
 */
package verins.GUI;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import verins.Utils.Comunes;
import verins.Utils.IOmethods;
import verins.Utils.Mensajes;

/**
 *
 * @author jpenam
 */
public class frmCargarRnv extends javax.swing.JFrame {

    /** Creates new form frmCargarRnv */
    String ruta="";
    public frmCargarRnv() {
        initComponents();
        Comunes.centrarVentana(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtArchivo = new javax.swing.JTextField();
        lblImagen1 = new javax.swing.JLabel();
        btnBusqArch = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Carga RNV");

        txtArchivo.setEnabled(false);

        lblImagen1.setText("Archivo");

        btnBusqArch.setText("...");
        btnBusqArch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBusqArchActionPerformed(evt);
            }
        });

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Carga de firmas y huellas");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblImagen1)
                        .addGap(18, 18, 18)
                        .addComponent(txtArchivo, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                        .addGap(34, 34, 34)
                        .addComponent(btnBusqArch, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(21, 21, 21))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(456, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBusqArch)
                    .addComponent(txtArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblImagen1))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnAceptar))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBusqArchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBusqArchActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text", "txt");
        fileChooser.setFileFilter(filter);
        System.out.println(verins.Utils.Comunes.getPathRNV());
        fileChooser.setCurrentDirectory(new File(verins.Utils.Comunes.getPathRNV()));
        int status = fileChooser.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            txtArchivo.setText(selectedFile.getPath());
            //jPanel1.cargarImagen(txtImagen1.getText());
        } else if (status == JFileChooser.CANCEL_OPTION) {
            txtArchivo.setText("");
        }
}//GEN-LAST:event_btnBusqArchActionPerformed
   
    public boolean LeeArchivo (String ruta){
      File archivo = null;
      FileReader fr = null;
      BufferedReader br = null;

      try {
         
         archivo = new File (ruta);
         fr = new FileReader (archivo);
         br = new BufferedReader(fr);

         // Lectura del fichero
         String linea;
         
         String DNI;
         String rutafin = ruta;
         int pos3=rutafin.lastIndexOf("/");
         rutafin=rutafin.substring(0, pos3+1);
         
         System.out.println(rutafin);
         while((linea=br.readLine())!=null){
             System.out.println(linea);
             if(linea.length() <=0)
                 break;
             
             if(linea.charAt(0) != 'H'){
                 DNI=linea.substring(1,9);
                // System.out.println(DNI);
                 //System.out.println(verins.Utils.Comunes.getPathRNV()+""+linea);
                 byte [] img = IOmethods.convertFileToByte(rutafin+""+linea); 
                 IOmethods.cargarFirmaRNV(img, DNI);
             }
             else
             {
                 
                 DNI=linea.substring(1, 9);
                 byte [] img = IOmethods.convertFileToByte(rutafin+""+linea); 
                 IOmethods.cargarHuellaRNV(img, DNI);
             }
         }
         
         //grabar padron
         return true;
      }
      catch(Exception e){
         e.printStackTrace();
         return false;
      }finally{
         // En el finally cerramos el fichero, para asegurarnos
         // que se cierra tanto si todo va bien como si salta 
         // una excepcion.
         try{                    
            if( null != fr ){   
               fr.close();     
            }                  
         }catch (Exception e2){ 
            e2.printStackTrace();
         }
      }
    }
    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
            // TODO add your handling code here:
        
        if (txtArchivo.getText().equals("")){
            Mensajes.alerta("Debe seleccionar un archivo");
            return;
        }
        if(Mensajes.guardar(evt, this))
            if (verins.Utils.Mensajes.exito(LeeArchivo(txtArchivo.getText())))
                dispose();
        
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmCargarRnv().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnBusqArch;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblImagen1;
    private javax.swing.JTextField txtArchivo;
    // End of variables declaration//GEN-END:variables
}
